package com.zzyl.nursing.service.impl;

import cn.hutool.core.collection.ListUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzyl.framework.web.service.TokenService;
import com.zzyl.nursing.domain.FamilyMember;
import com.zzyl.nursing.dto.UserLoginRequestDto;
import com.zzyl.nursing.mapper.FamilyMemberMapper;
import com.zzyl.nursing.service.IFamilyMemberService;
import com.zzyl.nursing.service.IWechatService;
import com.zzyl.nursing.vo.LoginVo;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

@Service
public class IFamilyMemberServiceImpl implements IFamilyMemberService {

    @Autowired
    private FamilyMemberMapper familyMemberMapper;
    @Autowired
    private IWechatService wechatService;

    @Autowired
    private TokenService tokenService;

    @Override
    public LoginVo login(UserLoginRequestDto dto) {
        String code = dto.getCode();
        String phoneCode = dto.getPhoneCode();

        //调用微信接口返回openid
        String openid = wechatService.getOpenid(code);


        //根据openid查询客户信息
        LambdaQueryWrapper<FamilyMember> queryWrapper = new LambdaQueryWrapper<FamilyMember>();
        queryWrapper.eq(FamilyMember::getOpenId, openid);
        FamilyMember familyMember = familyMemberMapper.selectOne(queryWrapper);

        String phone = wechatService.getPhone(phoneCode);

        //没有查到则进行添加
        if (familyMember == null) {
            familyMember = new FamilyMember();

            familyMember.setPhone(phone);
            familyMember.setName(DEFAULT_NICKNAME_PREFIX.get(new Random().nextInt(DEFAULT_NICKNAME_PREFIX.size()))+phone.substring(7));
            familyMember.setOpenId(openid);
            familyMemberMapper.insert(familyMember);
        }else{//查到进行修改


            if(! familyMember.getPhone().equals(phone)){
                familyMember.setPhone(phone);
            }
            familyMemberMapper.updateById(familyMember);
        }

        //调用返回token
        Map<String, Object> map = new HashMap<>();
        map.put("id",familyMember.getId() );
        map.put("nickName",familyMember.getName());
        String token = tokenService.createToken(map);

        //进行信息的插入
        LoginVo loginVo = new LoginVo();
        loginVo.setToken(token);
        loginVo.setNickName(familyMember.getName());


        //1、调用微信的接口返回一个openid
        //2、根据openid查询家属
        //3、调用微信的接口获取用户的手机号
        //4、如果查不到直接新增
        //5、判断手机号是否有变化，如果有变化更新家属信息
        //6、生成token
        return loginVo;
    }

    static List<String> DEFAULT_NICKNAME_PREFIX = ListUtil.of("生活更美好",
            "大桔大利",
            "日富一日",
            "好柿开花",
            "柿柿如意",
            "一椰暴富",
            "大柚所为",
            "杨梅吐气",
            "天生荔枝"
    );

//    @Override
//    public LoginVo login(UserLoginRequestDto dto) {
//
//        //1、调用微信的接口返回一个openid
//     String openid =    wechatService.getOpenid(dto.getCode());
//        //2、根据openid查询家属
//        LambdaQueryWrapper<FamilyMember> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(FamilyMember::getOpenId,openid);
//        FamilyMember familyMember = familyMemberMapper.selectOne(queryWrapper);
//        //3、调用微信的接口获取用户的手机号
//                String phone =     wechatService.getPhone(dto.getPhoneCode());
//        //4、如果查不到直接新增
//        if(familyMember == null){
//             familyMember = new FamilyMember();
//            familyMember.setPhone(phone);
//            familyMember.setName();
//            familyMember.setOpenId(openid);
//
//        }else{
//            //5、判断手机号是否有变化，如果有变化更新家属信息
//            if(familyMember.getPhone().equals(phone)){
//                familyMember.setOpenId(openid);
//
//            }
//              familyMemberMapper.updateById(familyMember);
//        }
//
//
//        //6、生成token
//        Map<String,Object> map = new HashMap<>();
//        map.put("id",familyMember.getId());
//        map.put("nickName",familyMember.getName());
//        String token = tokenService.createToken(map);
//
//        LoginVo loginVo = new LoginVo();
//        loginVo.setToken(token);
//        loginVo.setNickName(familyMember.getName());
//        return loginVo;
//    }
}
